// pages/editMine/editMine.js
const app = getApp()
const { updatePassword, updateUserInfo } = require("../../utils/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myTitle:"个人中心",
    navBarHeight:app.globalData.navBarHeight,
    userInfo: null,
    editMine:{
      popupHidden: true,
      overlayShow: false,
      info: {
        name: '',
        address: ''
      }
    },
    editPassword: {
      popupHidden: true,
      overlayShow: false,
      info: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: '',
      }
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { 
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })
  },
  handelback(){
    console.log('1')
    wx.navigateBack({
      delta:1
    })
  },
  openEditMine(){
    this.setData({ 
      'editMine.popupHidden':false,
      'editMine.overlayShow': true
    })
  },
  closeEditMine(){
    this.setData({ 
      'editMine.popupHidden':true,
      'editMine.overlayShow': false,
      'editMine.info.name': '',
      'editMine.info.address': ''
    })
  },
  submitEditMine(){
    const userInfo = this.data.editMine.info
    if(userInfo.name === '' || userInfo.address === '') {
      wx.showToast({
        title: '请输入有效信息！',
        icon: 'none'
      })
      return
    }
    updateUserInfo({PhoneNumber: this.data.userInfo.PhoneNumber,
    UserName: this.data.editMine.info.name,
    City: this.data.editMine.info.address}).then(res => {
      wx.showToast({
        title: res.msg+ '&重启应用后生效!',
        icon: "none"
      })
    })
    this.closeEditMine()
  },

  inputName(e) {
    this.setData({
      'editMine.info.name': e.detail.value
    })
  },
  inputAddress(e) {
    this.setData({
      'editMine.info.address': e.detail.value
    })
  },
  openEditPassword(){
    this.setData({ 
      'editPassword.popupHidden':false,
      'editPassword.overlayShow': true
    })
  },
  closeEditPassword(){
    this.setData({ 
      'editPassword.popupHidden':true,
      'editPassword.overlayShow': false,
      'editPassword.info.oldPassword': '',
      'editPassword.info.newPassword': '',
      'editPassword.info.confirmPassword': ''
    })
  },
  submitEditPassword(){
    const password = this.data.editPassword.info
    if(password.newPassword !== password.confirmPassword) {
      wx.showToast({
        title: '新密码两次输入不一致！请检查！',
        icon: "none"
      })
      return
    }
    updatePassword({PhoneNumber: this.data.userInfo.PhoneNumber,
      OldPassword: password.oldPassword,
      NewPassword: password.newPassword}).then(res => {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      })
    this.closeEditPassword()
  },

  inputOldPassword(e) {
    this.setData({
      'editPassword.info.oldPassword': e.detail.value
    })
  },
  inputNewPassword(e) {
    this.setData({
      'editPassword.info.newPassword': e.detail.value
    })
  },
  inputConfirmPassword(e) {
    this.setData({
      'editPassword.info.confirmPassword': e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})